/*
package org.example.util;

import cn.hutool.json.JSONUtil;

import javax.print.*;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.PrinterIsAcceptingJobs;
import javax.print.attribute.standard.PrinterState;
import javax.print.attribute.standard.PrinterStateReason;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class TestUtil {


    public static void main(String[] args) {

        // 1. 查找打印机
        PrintService printer = findPrinter("HPRT N31C");

        if (printer == null) {
            System.err.println("未找到汉印 N31C 打印机");
            return;
        }

        System.out.println("打印机名称: " + printer.getName());
        System.out.println("打印机端口: " + printer.getAttributes());

        checkPrinterStatus(printer);

        // 2. 准备打印内容 (使用 ESC/POS 指令)
        byte[] printData = createEscPosData2();
        System.out.println("打印数据长度: " + printData.length); // 调试信息

        System.out.println("打印内容的十六进制表示:");
        for (byte b : printData) {
            System.out.printf("%02X ", b);
        }
        System.out.println();

        // 3. 创建打印作业
        DocPrintJob job = printer.createPrintJob();
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        InputStream is = new ByteArrayInputStream(printData);
        Doc doc = new SimpleDoc(is, flavor, null);

        // 5. 添加打印监听器
        job.addPrintJobListener(new PrintJobAdapter() {
            @Override
            public void printJobFailed(PrintJobEvent pje) {
                System.err.println("打印任务失败: " + pje.getPrintEventType());
            }

            @Override
            public void printJobCompleted(PrintJobEvent pje) {
                System.out.println("打印任务完成");
            }

            @Override
            public void printJobCanceled(PrintJobEvent pje) {
                System.out.println("打印任务取消");
            }
        });

        // 4. 设置打印属性
        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        attributes.add(new Copies(1)); // 打印份数

        // 5. 执行打印
        try {
            job.print(doc, attributes);
            System.out.println("打印任务已发送");
        } catch (PrintException e) {
            e.printStackTrace();
        }
    }


    private static byte[] createEscPosData2() {
        try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
            buffer.write(0x1B); // ESC
            buffer.write('@');  // 初始化打印机
            String content = "简单测试\n"; // 简单的文本
            buffer.write(content.getBytes("GBK"));

            return buffer.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException("生成ESC/POS指令失败", e);
        }
    }

    private static byte[] createEscPosData3() {
        try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
            // ESC/POS 指令
            buffer.write(0x1B); // ESC
            buffer.write('@');   // 初始化打印机

            // 添加文本内容（使用GBK编码）
            String content = "第一个测试\n"; // 包含换行
            buffer.write(content.getBytes("GBK"));

            // 走纸并切刀 (根据打印机型号调整)
            buffer.write(0x1D); // GS
            buffer.write('V');  // 切刀指令
            buffer.write(66);    // 切刀模式
            buffer.write(3);     // 走纸行数

            return buffer.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException("生成ESC/POS指令失败", e);
        }

        //return "!Hello HPRT!\n".getBytes(StandardCharsets.ISO_8859_1);
    }


    public static void checkPrinterStatus(PrintService printer) {
        AttributeSet attributes = printer.getAttributes();

        // 检查打印机状态
        PrinterState state = (PrinterState) attributes.get(PrinterState.class);
        System.out.println("打印机状态: " + state);

        // 检查错误原因
        PrinterStateReason stateReasons = (PrinterStateReason)
                attributes.get(PrinterStateReason.class);

        if (stateReasons != null) {
            System.out.println("打印机状态原因:"+ JSONUtil.toJsonStr(stateReasons));
            // 遍历所有状态原因
           */
/* for (PrinterStateReason reason : stateReasons.values()) {
                Severity severity = stateReasons.getSeverity(reason);
                System.out.println("- " + severity + ": " + reason);
            }*//*

        }

        // 检查是否接受作业
        boolean acceptingJobs = printer.getAttribute(PrinterIsAcceptingJobs.class)
                .equals(PrinterIsAcceptingJobs.ACCEPTING_JOBS);
        System.out.println("是否接受作业: " + acceptingJobs);
    }

    private static PrintService findPrinter(String printerName) {
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        for (PrintService service : services) {
            System.out.println("打印机名称："+service.getName());
            if (service.getName().contains(printerName)) {
                return service;
            }
        }
        return null;
    }

    private static byte[] createEscPosData() {
        StringBuilder sb = new StringBuilder();

        // ESC/POS 指令集
        sb.append((char) 0x1B).append("@"); // 初始化打印机
        sb.append((char) 0x1B).append("a").append((char) 1); // 居中
        sb.append((char) 0x1D).append("!").append((char) 0x11); // 设置字体大小 (双倍宽高)

        // 标题
        sb.append("=== 汉印 N31C 打印测试 ===\n\n");

        // 重置格式
        sb.append((char) 0x1D).append("!").append((char) 0x00);
        sb.append((char) 0x1B).append("a").append((char) 0x00);

        // 内容
        sb.append("订单号: ORD20230721001\n");
        sb.append("日期: ").append(new java.util.Date()).append("\n");
        sb.append("商品: 无线鼠标 x2\n");
        sb.append("总价: ￥199.90\n\n");

        // 二维码
        sb.append((char) 0x1B).append("a").append((char) 1); // 居中
        sb.append("扫描查看详情\n");
        addQrCode(sb, "https://example.com/order/ORD20230721001");

        // 切纸
        sb.append((char) 0x1D).append("V").append((char) 66).append((char) 0);

        return sb.toString().getBytes(StandardCharsets.ISO_8859_1);
    }

    private static void addQrCode(StringBuilder sb, String data) {
        // QR 码设置
        sb.append((char) 0x1D).append("(k")
                .append((char) 4).append((char) 0).append((char) 49).append((char) 65).append((char) 50).append((char) 0);

        sb.append((char) 0x1D).append("(k")
                .append((char) 3).append((char) 0).append((char) 49).append((char) 67).append((char) 4);

        sb.append((char) 0x1D).append("(k")
                .append((char) 3).append((char) 0).append((char) 49).append((char) 69).append((char) 49);

        // QR 码数据
        int len = data.length() + 3;
        sb.append((char) 0x1D).append("(k")
                .append((char) (len % 256)).append((char) (len / 256))
                .append((char) 49).append((char) 80).append((char) 48);
        sb.append(data);

        // 打印 QR 码
        sb.append((char) 0x1D).append("(k")
                .append((char) 3).append((char) 0).append((char) 49).append((char) 81).append((char) 48);
    }

}
*/
